## -*- coding: utf-8 -*-
#pylint: disable-msg=W0212
'''
Created on Oct 8, 2009

@author: sax
'''
from django.core.management.base import NoArgsCommand
from django.core.management.color import color_style
from django.core.urlresolvers import reverse, resolve
from django.conf import settings
from django.template.loader import get_template
from django.contrib.admin import site
from django.contrib import admin
admin.autodiscover() 
from xadmin.admin import XModelAdmin
import os

style = color_style()

def _test(test_msg, test, ok_msg="ok", err_msg="error"):
    print test_msg,
    if test:
        print style.SQL_FIELD( ok_msg )
    else:
        print style.ERROR( err_msg )

def _assert(test_msg, test,ok_msg="ok", err_msg="error", *args, **kwargs):
    print test_msg,
    if kwargs:
        help = kwargs.pop("help")
    else:
        help = "" 
    try:
        test(*args, **kwargs)
        print style.SQL_FIELD( ok_msg )
    except Exception, e:
        print style.ERROR( "%s (%s)" % (err_msg, str(e)) ),
        print help
    
class Command(NoArgsCommand):
    help = "Executes ``sqlflush`` on the current database."
    
    def test_template(self):
        from django.test.client import Client
        from django.test.utils import setup_test_environment
        setup_test_environment()
        for k,v in site._registry.items():            
            info = v.model._meta.app_label, v.model._meta.module_name, "changelist"
            view_name = "_".join(info)
            url =  reverse("admin:"+view_name)                        
            c = Client()
            c.login(username='xadmin',password='xadmin')
            ret = c.get(url)
            names = [x.name for x in ret.template ]
            assert u'xadmin/base.html' in names
    
    def test_media(self, uri):
        from django.test.client import Client
        c = Client()
        ret = c.get(uri)
        assert ret.status_code == 200
        
    def handle_noargs(self, **options):
        self.style = color_style()
        print "Checking settings:" 
        _test("  XADMIN_MEDIA_ROOT", hasattr(settings,'XADMIN_MEDIA_ROOT') )
        _test("  XADMIN_MEDIA_URL", hasattr(settings,'XADMIN_MEDIA_URL') )
        _test("  XADMIN_MEDIA_ROOT location", os.path.isdir(settings.XADMIN_MEDIA_ROOT) )
                
        
        print "Checking all registered models should be managed by XModelAdmin:"
        for k,v in site._registry.items():
            _assert("  check %s " % k, isinstance, "ok", "error", v, XModelAdmin )
        
        print "Checking templates:"
        _assert("  loading xadmin templates", get_template, "ok", "error", 'xadmin/inlines/ui_button.html')
        _assert("  check right inheritance", self.test_template, "ok", "error")
        print "Checking media:"
         
        uri_css = "%scss/xforms.css" % settings.XADMIN_MEDIA_URL
        uri_js = "%sjs/jquery/jquery.autocomplete.pack.js" % settings.XADMIN_MEDIA_URL
        uri_img = "%simg/external.png" % settings.XADMIN_MEDIA_URL
         
        _assert("  check url conf",resolve, "ok", "error", settings.XADMIN_MEDIA_URL, help="cannot resolve '%s'" %  settings.XADMIN_MEDIA_URL)
        _assert("  check media availability (css)",self.test_media, "ok", "error", uri_css, help="cannot wget '%s'" % uri_css )
        _assert("  check media availability (js)",self.test_media, "ok", "error", uri_js, help="cannot wget '%s'" % uri_js )
        _assert("  check media availability (images)",self.test_media, "ok", "error", uri_js, help="cannot wget '%s'" % uri_img )
        

        
            